package com.heima.a07_demo.server.mapper;

import com.heima.a07_demo.pojo.entity.DiagnosisRecord;
import com.heima.a07_demo.pojo.vo.DiagnosisRecordVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface DiagnosisRecordMapper {
      /*
    * 保存诊断记录
    * */

    @Insert("insert into diagnosis_record (record_id,doctor_id, patient_id, left_result, right_result, final_result," +
            " left_confidence, right_confidence, diagnosis_time, diagnosis_summary)" +
            " values "+
    "(#{recordId},#{doctorId},#{patientId},#{leftResult},#{rightResult},#{finalResult}," +
            "#{leftConfidence}," +
            "#{rightConfidence},#{diagnosisTime},#{diagnosisSummary})")
    @Options(
            useGeneratedKeys = true,  // 启用自增主键回填
            keyProperty = "recordId",  // 指定实体类中的 ID 字段名
            keyColumn = "record_id"    // 指定数据库中的主键列名（可选，默认与 keyProperty 同名）
    )
    void save(DiagnosisRecord diagnosisRecord);
    // 查询今日诊断记录数
    @Select("""
        SELECT COUNT(*)
        FROM test_a07.diagnosisrecord
        WHERE DATE(diagnosis_time) = CURDATE()
    """)
    Integer countTodayRecord(Long id);

    /**
     * 分页查询诊断记录
     * @param beginTime 起始时间
     * @param endTime 结束时间
     * @param diagnosisType 疾病类型
     * @param offset 偏移量
     * @param pageSize 每页记录数
     * @return 诊断记录列表
     */
    List<DiagnosisRecordVO> selectDiagnosisRecordByPage(
            @Param("beginTime") String beginTime,
            @Param("endTime") String endTime,
            @Param("diagnosisType") String diagnosisType,
            @Param("offset") int offset,
            @Param("pageSize") int pageSize
    );

    /**
     * 查询诊断记录总数
     * @param beginTime 起始时间
     * @param endTime 结束时间
     * @param diagnosisType 疾病类型
     * @return 诊断记录总数
     */
    Long selectDiagnosisRecordCount(
            @Param("beginTime") String beginTime,
            @Param("endTime") String endTime,
            @Param("diagnosisType") String diagnosisType
    );

    // 查询昨日诊断记录数
    @Select("""
        SELECT COUNT(*)
        FROM test_a07.diagnosisrecord
        WHERE DATE(diagnosis_time) = CURDATE() - INTERVAL 1 DAY
    """)
    Integer countYesterdayRecord(Long id);

    // 查询累计诊断记录数
    @Select("""
        SELECT COUNT(*)
        FROM test_a07.diagnosisrecord
    """)
    Integer countAllRecord(Long id);

    // 查询上月诊断记录数
    @Select("""
        SELECT COUNT(*)
        FROM test_a07.diagnosisrecord
        WHERE YEAR(diagnosis_time) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
          AND MONTH(diagnosis_time) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
    """)
    Integer countLastMonthRecord(Long id);

    // 查询今日新增患者数量
    @Select("""
        SELECT COUNT(DISTINCT patient_id)
        FROM test_a07.diagnosisrecord
        WHERE DATE(diagnosis_time) = CURDATE()
          AND patient_id IS NOT NULL
    """)
    Integer countTodayPatient(Long id);

    // 查询昨日新增患者数量
    @Select("""
        SELECT COUNT(DISTINCT patient_id)
        FROM test_a07.diagnosisrecord
        WHERE DATE(diagnosis_time) = CURDATE() - INTERVAL 1 DAY
          AND patient_id IS NOT NULL
    """)
    Integer countYesterdayPatient(Long id);

    // 查询病例总数
    @Select("""
        SELECT COUNT(DISTINCT record_id)
        FROM test_a07.diagnosisresult
    """)
    Integer countCaseNum(Long id);

    // 查询上月病例总数
    @Select("""
        SELECT COUNT(DISTINCT record_id)
        FROM test_a07.diagnosisresult
        WHERE YEAR(diagnosis_time) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
          AND MONTH(diagnosis_time) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
    """)
    Integer countLastMonthCaseNum(Long id);
}

